package com.zmn.oms.zmn.facade.impl.order;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.bo.order.OrderBO;
import com.zmn.oms.model.dto.order.create.CallCenterOrderCluesDTO;
import com.zmn.oms.model.dto.order.create.CreateOrderDTOH;
import com.zmn.oms.model.dto.order.create.CreateOrderFailMessage;
import com.zmn.oms.model.dto.order.zmn.ZsNormalNewOrderDTO;
import com.zmn.oms.model.dto.product.OrderProductDTO;
import com.zmn.oms.third.base.util.ThirdOrderUtil;
import com.zmn.oms.third.callcenter.dto.PushOrderCluesDTO;
import com.zmn.oms.third.callcenter.service.CallCenterService;
import com.zmn.oms.zmn.business.interfaces.order.CreateOrderBService;
import com.zmn.oms.zmn.facade.interfaces.order.CreateOrderFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/02/01 13:56
 */
@Slf4j
@Service("createOrderFacade")
public class CreateOrderFacadeImpl implements CreateOrderFacade {

    @Autowired
    private CreateOrderBService createOrderBService;
    @Autowired
    private CallCenterService callCenterService;

    /**
     * 通过呼叫中心推送过来的订单线索下单
     * @param callCenterOrderCluesDTO
     * @return
     * @throws OmsBaseException
     */
    // TODO -lhl 事务及日志待重构，使用新的日志和事务前需要剔除旧的日志和事务
    @Override
    public ResultDTO<OrderBO> createOrderByCallCenterOrderClues(CallCenterOrderCluesDTO callCenterOrderCluesDTO) throws OmsBaseException {
        CreateOrderDTOH createOrderDTOH = createOrderBService.prepareCreateOrderByCallCenterOrderClues(callCenterOrderCluesDTO);

        ZsNormalNewOrderDTO newOrderDTO = createOrderDTOH.getNewOrderDTO();
        OrderProductDTO orderProductDTO = newOrderDTO.getOrderProductList().get(0);
        if (newOrderDTO.getLongitude() == null || newOrderDTO.getLatitude() == null
                || Objects.equals(OrderConsts.CHANNEL_DISABLED_PRODUCT_ID, orderProductDTO.getShowProductId())) {
            // 没匹配上城市或产品，不创建新单，打回呼叫中心处理
            return ResultDTO.fail("未匹配到产品");
        }

        OrderBO orderBO = createOrderBService.doCreateOrder(createOrderDTOH);

        return ResultDTO.success(orderBO);
    }

    @Override
    public ResultDTO onCreateOrderFail(CreateOrderFailMessage createOrderFailMessage) {
        // 提取订单线索，发送到呼叫中心处理
        PushOrderCluesDTO pushOrderCluesDTO = new PushOrderCluesDTO();
        BeanUtils.copyProperties(createOrderFailMessage, pushOrderCluesDTO);
        pushOrderCluesDTO.setUserPhone(createOrderFailMessage.getTelephone());
        pushOrderCluesDTO.setUserName(createOrderFailMessage.getContactName());
        //pushOrderCluesDTO.setRemark(createOrderFailMessage.getExtData());

        ResultDTO resultDTO = callCenterService.pushOrderClues2CallCenter(pushOrderCluesDTO);

        return resultDTO;
    }
}
